#include "sockets_imp.hpp"
#include <string>

NetAddress::NetAddress()
{
	validData = false;
}

void NetAddress::resolve( std::string const& hostname, int port )
{
	hostent* p = gethostbyname( hostname.c_str() );
	
	if(!p)
	{
		validData = false;
		return;
	}
	
	//TODO not assume ipv4
	
	sockaddr_in* s = reinterpret_cast<sockaddr_in*>(&data);
	
	memset(reinterpret_cast<char *>(s->sin_zero), 0, sizeof(s->sin_zero));
	memmove(reinterpret_cast<char *>(&s->sin_addr), p->h_addr_list[0], p->h_length);
	s->sin_family = p->h_addrtype;
	s->sin_port = htons( (u_short)port );
	
	validData = true;
}

bool NetAddress::isValid() const
{
	return validData;
}
